package org.oos.dao;

import java.util.ArrayList;

import org.oos.data.UserService;
import org.oos.rome.WSSEAuthStrategy;
import org.oos.services.OOS4Java;
import org.w3c.dom.Element;

import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.propono.atom.client.AtomClientFactory;
import com.sun.syndication.propono.atom.client.ClientAtomService;
import com.sun.syndication.propono.atom.client.ClientCollection;
import com.sun.syndication.propono.atom.client.ClientEntry;
import com.sun.syndication.propono.atom.client.ClientWorkspace;

public class UserServiceDao extends ServiceDao
{
	
	public UserServiceDao(OOS4Java oos)
	{
		super(oos);
	}
	public UserServiceDao()
	{
		super();
	}
	
	public UserService getService(Element entry) throws Exception
	{
		UserService service = new UserService();
		populateCommonfields(service, entry);
		service.summary = atom.getElementValue(entry, "summary");
		return service;
	}
	
	public void create()
	{
		try
		{
	        ClientAtomService service = AtomClientFactory.getAtomService(oos.API_BASE_URL, new WSSEAuthStrategy(oos.EMAIL, oos.AUTH_TOKEN));
	        ClientWorkspace wkspace = (ClientWorkspace) service.getWorkspaces().get(0);
	        
	        ClientCollection sites = null;
	        for (Object o: wkspace.getCollections())
	        {
	        	ClientCollection cc = (ClientCollection) o;
	        	if ("Sites".equals(cc.getTitle())) sites = cc;
	        	System.out.println(cc.getTitle()+"=>"+cc.getTitleType());
	        }
	        ClientEntry entry = sites.createEntry();
	        entry.setTitle("ttitieriweirwier");
	        entry.setContent("asdasdasdasdasdasdas", "application/atom+xml");
	        
	        Content c = new Content();
	        c.setSrc("oos:telephone");
	        c.setValue("234234234");
	        ArrayList<Content> cs = new ArrayList<Content>();
	        cs.add(c);
	        ArrayList<String> fm = new ArrayList<String>();
	        fm.add("<oos:telephone>asdadadsa</oos:telephone>");
	        entry.setForeignMarkup(fm);
	        entry.setContents(cs);
	        
	        sites.addEntry(entry);
		}
		catch(Exception e)
		{
			log.e("asdasd", "asdasd", e);
		}
	}
}
